package com.manjiwang.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.manjiwang.goods.entity.Goods;
import com.manjiwang.goods.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商品控制层
 *
 * @Author Yang Hai Feng
 * @Date 2021/8/10 20:17
 */
@RestController
@RequestMapping(value = "/goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 查询商品
     *
     * @param id
     * @return
     */
    @GetMapping("/getGoods")
    public JSONObject getGoods(@RequestParam("id") String id) {
        Goods goods = goodsService.getById(id);
        return JSONObject.parseObject(JSONObject.toJSONString(goods));
    }

    /**
     * 模拟减库存，每次商品数量-1
     *
     * @param id
     * @return
     */
    @GetMapping("/updateGoods")
    public JSONObject updateGoods(@RequestParam("id") String id) {
        Goods goods = goodsService.getById(id);
        goods.setCount(goods.getCount() - 1);
        goodsService.updateById(goods);
        return JSONObject.parseObject(JSONObject.toJSONString(goods));
    }


}
